
import UIKit
import CoreImage
import SwiftQRScanner

class MyVC: BaseVC {

    var imgArray : Array<Any> = []
    var AccountArray : Array<Any> = []
    var headImg : UIImageView = UIImageView()
    var nameLab : UILabel = UILabel()

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
        requestUserInfo(isRefresh: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.isNavigationBarHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.title = ""
        //"Start Scan",
//        dataSource = ["Generate QR code from string","History","Login account"]
        setup()
        requestUserInfo(isRefresh: false)
    }
    
    func requestUserInfo (isRefresh:Bool) {
        if let userinfo = gosnUser.getUserInfo(gosnUser.myId()) {
            self.headImg.sd_setImage(with: URL(string: userinfo.portrait), placeholderImage: kUserImage)
            self.nameLab.text = userinfo.getAlias()
        }
    }
    
    func setup () {
//        dataSource = ["推荐","收藏","关于","设置"]
//        imgArray = ["tuijian_icon","my1","about","my2"]
        
        dataSource = ["我的个人信息","我的二维码","推荐","关于","设置"]
        imgArray = ["myinfo","qr","myShare","about","setting"]
//        dataSource = ["关于","设置"]
//        imgArray = ["about","setting"]

        tableStytle = .insetGrouped
        tableView.dataSource = self
        tableView.delegate = self
        tableView.layer.cornerRadius = 10
        tableView.register(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")
        
        tableView.addExtrudeHeader(ofImage: #imageLiteral(resourceName: "me_bg"), height: UIScreen.main.bounds.height*0.3, { [self] (header) in
            self.headImg = UIImageView.init(frame: CGRect(x: kScreenWidth / 2 - 50, y: GKNavHeight, width: 100, height: 100))
            self.headImg.layer.masksToBounds = true
            self.headImg.layer.cornerRadius = 50
            self.headImg.contentMode = .scaleToFill
            self.headImg.image = UIImage(named: "head")
            header.addSubview(self.headImg)
            
            self.nameLab = UILabel()
            self.nameLab.textAlignment = .center
            self.nameLab.font = .systemFont(ofSize: 20)
            self.nameLab.frame = CGRect(x: kScreenWidth / 2 - 150.0, y: GKNavHeight + 120, width: 300, height: 30)
            self.nameLab.text = "defalut"
            header.addSubview(self.nameLab)
            header.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(pushMeSettingVC)))
        })
    }
    
    @objc func pushMeSettingVC () {
        self.navigationController?.pushViewController(MySetVC(), animated: true)
    }
    

    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 10))
        view.backgroundColor = UIColor.lightGray
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
        cell.contentView.backgroundColor = UIColor.clear
//        cell.selectionStyle = .none
        cell.img.image = UIImage(named: imgArray[indexPath.row] as! String)
        if indexPath.row < dataSource.count,
           let str = dataSource[indexPath.row] as? String,
           let titleLab = cell.titleLab{
            titleLab.text = str
            }
        
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)
        
  
//        if indexPath.row == 0 {
//            navigationController?.pushViewController(AboutVC(), animated: true)
//        }else if indexPath.row == 1 {
//            navigationController?.pushViewController(SettingVC(), animated: true)
//        }
        
        if indexPath.row == 0 {
            self.navigationController?.pushViewController(MySetVC(), animated: true)
        }else if indexPath.row == 1 {
            self.navigationController?.pushViewController(QRVC(with: DMCCConversation(), isGroup: false, isUser: false), animated: true)
        }else if indexPath.row == 2 {
//            let textToShare = "悦聊"
//            let imageToShare = UIImage(named: "logo")
//            let urlToShare = URL(string: "itms-apps://itunes.apple.com/app/id6670187575?")
//            let activityItems = [textToShare, imageToShare as Any, urlToShare as Any] as [Any]
//            let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
//            self.present(activityVC, animated: true, completion: nil)
            
            navigationController?.pushViewController(NearVC(), animated: true)
        }else if indexPath.row == 3 {
            navigationController?.pushViewController(AboutVC(), animated: true)
        }else if indexPath.row == 4 {
            navigationController?.pushViewController(SettingVC(), animated: true)
        }
        
//        if indexPath.row == 0 {
////            self.presentVC(VC: RecommendVC())
//            navigationController?.pushViewController(RecommendVC(), animated: true)
//        }else if indexPath.row == 1 {
////            self.presentVC(VC: HistoryVC())
//        }else if indexPath.row == 2 {
////            self.presentVC(VC: AboutVC())
//            navigationController?.pushViewController(AboutVC(), animated: true)
//        }else if indexPath.row == 3 {
//            navigationController?.pushViewController(SettingVC(), animated: true)
//        }
        
    }
    
}

